package com.lovo.Atm;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class JdbcUtil {
    /**
     * 链接数据库
     */
    public static Connection getConnection() {
        Connection con = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 2. 创建连接
            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/emp?userSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT",
                    "root", "qq1137545199");
            System.out.println("con:" + con);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return con;
    }


    /**
     * 封装close
     *
     * @param statement
     * @param con
     */
    public static void close(Statement statement, Connection con) {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    /**
     * 重载查询close
     *
     * @param statement
     * @param con
     * @param res
     */
    public static void close(Statement statement, Connection con, ResultSet res) {
        if (res != null) {
            try {
                res.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }


    /**
     * 增加 删除 修改 都是可以的 用同样的办法 创建了多少链接就要关闭多少
     * @return
     */
    public static void revise(String sql) {
        //1.创建链接
        Connection con = getConnection();
        Statement statement = null;
        try {
            //获取执行SQL语句对象
            statement = con.createStatement();
            //执行SQL语句
            int row = statement.executeUpdate(sql);
            System.out.println("影响的行数：" + row);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //关闭链接
            close(statement, con);
        }

    }



    /**
     * 查询 需要关闭三个链接 con, statement, res
     */
        public static ArrayList query() {
        //1.创建链接
        Connection con = getConnection();
        Statement statement = null;
        ResultSet res = null;
        List userList = new ArrayList();
        try {
            //2.获取查询对象
            statement = con.createStatement();
            String sql = "select * from t_user";
            //3.执行SQL语句并返回结果
            res = statement.executeQuery(sql);
            while (res.next()) {
                //最好写列名或者列别名 写序号不易分辨了
                User user = new User();
                user.setId(res.getInt("u_id"));
                user.setName(res.getString("u_name"));
                user.setPassword(res.getString("u_password"));
                user.setBlance(res.getDouble("u_blance"));
                userList.add(user);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭
            close(statement, con, res);
        }
        return (ArrayList)userList;
    }

}
